package io.renren.modules.base.service.impl;

import io.renren.common.utils.RandomId;
import io.renren.modules.base.dao.*;
import io.renren.modules.base.entity.BaseTaskWorkitemEntity;
import io.renren.modules.base.entity.BaseTemplateEntity;
import io.renren.modules.base.entity.BaseTemplateTaskEntity;
import io.renren.modules.base.service.BaseTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;


@Service("baseTemplateService")
public class BaseTemplateServiceImpl implements BaseTemplateService {
	@Autowired
	private BaseTemplateDao baseTemplateDao;
	@Autowired
	private BaseTemplateTaskDao templateTaskDao;
	@Autowired
	private BaseTaskInfoDao taskInfoDao;
	@Autowired
	private BaseTaskWorkitemDao taskWorkitemDao;
	@Autowired
	private BaseWorkitemInfoDao workitemInfoDao;
	
	@Override
	public BaseTemplateEntity queryObject(String id){
		return baseTemplateDao.queryObject(id);
	}
	
	@Override
	public List<BaseTemplateEntity> queryList(Map<String, Object> map){
		return baseTemplateDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return baseTemplateDao.queryTotal(map);
	}
	
	@Override
	public void save(BaseTemplateEntity baseTemplate){
		baseTemplate.setId(RandomId.createId()+"");
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(currentTime);
		baseTemplate.setCreationTime(dateString);
		baseTemplateDao.save(baseTemplate);
	}
	
	@Override
	public void update(BaseTemplateEntity baseTemplate){
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(currentTime);
		baseTemplate.setUpdateTime(dateString);
		baseTemplateDao.update(baseTemplate);
	}
	
	@Override
	public void delete(String id){
		baseTemplateDao.delete(id);
	}
	
	@Override
	public void deleteBatch(String[] ids){
		//删除模板表
		baseTemplateDao.deleteBatch(ids);
		//根据模板ID查询模板下面的任务
		List<BaseTemplateTaskEntity> templateTaskEntityList = templateTaskDao.queryTemplateId(ids);
		 List<String> templateTaskIdList = new ArrayList<>();
		 List<String> taskIdList = new ArrayList<>();
		if (templateTaskEntityList != null && templateTaskEntityList.size()>0){
            for (int i = 0 ; i < templateTaskEntityList.size() ; i++){
				templateTaskIdList.add(templateTaskEntityList.get(i).getId());
				taskIdList.add(templateTaskEntityList.get(i).getTaskId());
			}
			//删除模板与任务关联表
			String[] strings = new String[templateTaskIdList.size()];
			templateTaskDao.deleteBatch(templateTaskIdList.toArray(strings));
            //删除任务表
			String[] string = new String[taskIdList.size()];
			taskInfoDao.deleteBatch(taskIdList.toArray(string));
			//根据任务ID查询任务与任务工作项关联表
			List<BaseTaskWorkitemEntity> baseTaskWorkitemEntityList = taskWorkitemDao.queryTaskId(taskIdList.toArray(string));
			if (baseTaskWorkitemEntityList != null && baseTaskWorkitemEntityList.size()>0){
				List<String> taskWorkitemIdList = new ArrayList<>();
				List<String> workitemIdList = new ArrayList<>();
				for (int i = 0 ; i < baseTaskWorkitemEntityList.size();i++){
					taskWorkitemIdList.add(baseTaskWorkitemEntityList.get(i).getId());
					workitemIdList.add(baseTaskWorkitemEntityList.get(i).getWoekitmeId());
				}
				//删除任务与任务工作项关联表
				String[] taskWorkitemIds = new String[taskWorkitemIdList.size()];
				taskWorkitemDao.deleteBatch(taskWorkitemIdList.toArray(taskWorkitemIds));
				//删除工作项表
				String[] workitemIds = new String[workitemIdList.size()];
				workitemInfoDao.deleteBatch(workitemIdList.toArray(workitemIds));
			}

		}

	}

	@Override
	public List<BaseTemplateEntity> query(){return baseTemplateDao.query();}
	
}
